/*
	Adobe Systems Incorporated(r) Source Code License Agreement
	Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
	
	Please read this Source Code License Agreement carefully before using
	the source code.
	
	Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 
	no-charge, royalty-free, irrevocable copyright license, to reproduce,
	prepare derivative works of, publicly display, publicly perform, and
	distribute this source code and such derivative works in source or 
	object code form without any attribution requirements.  
	
	The name "Adobe Systems Incorporated" must not be used to endorse or promote products
	derived from the source code without prior written permission.
	
	You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
	against any loss, damage, claims or lawsuits, including attorney's 
	fees that arise or result from your use or distribution of the source 
	code.
	
	THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 
	ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  ALSO, THERE IS NO WARRANTY OF 
	NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT.  IN NO EVENT SHALL MACROMEDIA
	OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
	OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
	WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
	OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
	ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.adobe.webapis.mappr
{
	import flexunit.framework.TestCase;
	import flexunit.framework.TestSuite;
	
	import com.adobe.webapis.mappr.MapprService;
	import com.adobe.webapis.mappr.events.MapprResultEvent;
	import com.adobe.webapis.mappr.events.MapprServiceErrorEvent;
	
	import com.adobe.webapis.mappr.Place;
	import com.adobe.webapis.mappr.Photo;
	import com.adobe.webapis.mappr.Position;
	import com.adobe.webapis.mappr.Owner;
	import com.adobe.webapis.mappr.Tag;
	import flexunit.framework.Test;

	public class MapprServiceTest extends TestCase
	{
		/******************
			YOU MUST HAVE AN API KEY. 
			
			YOU CAN GET ONE AT
			http://www.mappr.com/about/api.phtml
		******************/
		public static var API_KEY:String;
		
		private const LIMIT:uint = 4;
		private const TAGS:Array = ["SF", "Boston"];
		private const CALL_TIMEOUT:Number = 30000;
		private const PLACE_ID:String = "1964";
		private const PER_PAGE:Number = 3;
		private const PAGE:Number = 2;
		private const GROUP:Boolean = false;
		
		private const USER_ID:String = null;
		private const PHOTO_ID:String = "2188580";
		
	    public function MapprServiceTest( methodName:String = null )
        {
            super(methodName);
        }
        
		public function testAPIKeyIsSet():void
		{
			assertNotNull("API Key not set. You must specify it in com.adobe.webapis.mappr.tests.MapprServiceTest", MapprServiceTest.API_KEY);
		}
		
		public function testGetPhotoInfo():void
		{
			var m:MapprService = new MapprService(MapprServiceTest.API_KEY);
				m.addEventListener(MapprResultEvent.ON_GET_PHOTO_INFO, addAsync(onGetPhotoInfo, CALL_TIMEOUT));
			
				m.getPhotoInfo(PHOTO_ID);			
		}
		
		private function onGetPhotoInfo(result:MapprResultEvent):void
		{
			assertNotNull("result.data.photosis null", result.data.photo);
			
			var photo:Photo = result.data.photo;
			
			assertNotNull("photo.date is null", photo.date);
			assertNotNull("photo.match is null", photo.match);
			assertNotNull("photo.thumbnail is null", photo.thumbnail);
			assertNotNull("photo.id is null", photo.id);
			assertNotNull("photo.link is null", photo.link);
			
			assertNotNull("photo.place is null", photo.place);
			
			assertNotNull("photo.place.id is null", photo.place.id);
			assertNotNull("photo.place.name is null", photo.place.name);
			
			var pos:Position = photo.place.position;
			
			assertNotNull("position is null", pos);
			assertNotNull("pos.latitude is null", pos.latitude);
			assertNotNull("pos.longitude is null", pos.longitude);
			assertNotNull("pos.spread is null", pos.spread);
			
			assertNotNull("photo.projected is null", photo.projected);
			//assertTrue("photo.projected.spread != 0", photo.projected.spread != 0);
			assertTrue("photo.projected.x != 0", photo.projected.x != 0);
			assertTrue("photo.projected.y != 0", photo.projected.y != 0);
			assertTrue("photo.projected.theta != 0", photo.projected.theta != 0);
			
			var owner:Owner = photo.owner;
			
			assertNotNull("owner is null", owner);
			assertNotNull("owner.icon is null", owner.icon);
			assertNotNull("owner.link is null", owner.link);
			assertNotNull("owner.nsid is null", owner.nsid);
			assertNotNull("owner.username is null", owner.username);	
			
			var tags:Array = photo.tags;	
			
			assertNotNull("photo.tags is null", tags);
			assertTrue("tags.length > 0", tags.length > 0);
		}
		
		private function onAPIError(event:MapprServiceErrorEvent):void
		{
			var errorString:String = "MapprServiceErrorEvent : [" + event.text + "] : Code : " +
									event.code + " Message : " + event.message;
			fail(errorString);
		}
		
		public function testSearch():void
		{
			var m:MapprService = new MapprService(MapprServiceTest.API_KEY);
				m.addEventListener(MapprResultEvent.ON_SEARCH, addAsync(onSearch, CALL_TIMEOUT));
				m.addEventListener(MapprServiceErrorEvent.ON_API_ERROR, onAPIError);
			
				m.search(TAGS[0], USER_ID, null, PER_PAGE, PAGE, GROUP);			
		}
		
		private function onSearch(result:MapprResultEvent):void
		{
			assertNotNull("result.data.photos is null", result.data.photos);
			assertTrue("result.data.page == PAGE", result.data.page == PAGE);
			assertTrue("result.data.perPage == PER_PAGE", result.data.perPage == PER_PAGE);
			
			assertTrue("result.data.photos.length > 0", result.data.photos.length > 0);
			
			var photo:Photo = result.data.photos[0];
			
			assertNotNull("photo.date is null", photo.date);
			assertNotNull("photo.match is null", photo.match);
			assertNotNull("photo.thumbnail is null", photo.thumbnail);
			assertNotNull("photo.id is null", photo.id);
			assertNotNull("photo.link is null", photo.link);
			
			assertNotNull("photo.place is null", photo.place);
			
			assertNotNull("photo.place.id is null", photo.place.id);
			assertNotNull("photo.place.name is null", photo.place.name);
			
			var pos:Position = photo.place.position;
			
			assertNotNull("position is null", pos);
			assertNotNull("pos.latitude is null", pos.latitude);
			assertNotNull("pos.longitude is null", pos.longitude);
			assertNotNull("pos.spread is null", pos.spread);
			
			assertNotNull("photo.projected is null", photo.projected);
			//assertTrue("photo.projected.spread != 0", photo.projected.spread != 0);
			assertTrue("photo.projected.x != 0", photo.projected.x != 0);
			assertTrue("photo.projected.y != 0", photo.projected.y != 0);
			assertTrue("photo.projected.theta != 0", photo.projected.theta != 0);
			
			var owner:Owner = photo.owner;
			
			assertNotNull("owner is null", owner);
			assertNotNull("owner.icon is null", owner.icon);
			assertNotNull("owner.link is null", owner.link);
			assertNotNull("owner.nsid is null", owner.nsid);
			assertNotNull("owner.username is null", owner.username);			
		}
		
		public function testGetRecent():void
		{
			var m:MapprService = new MapprService(MapprServiceTest.API_KEY);
				m.addEventListener(MapprResultEvent.ON_GET_RECENT, addAsync(onGetRecent, CALL_TIMEOUT));
			
				m.getRecent(PLACE_ID, PER_PAGE, PAGE, GROUP);
		}
		
		private function onGetRecent(result:MapprResultEvent):void
		{
			assertNotNull("result.data.photos is null", result.data.photos);
			assertTrue("result.data.page == PAGE", result.data.page == PAGE);
			assertTrue("result.data.perPage == PER_PAGE", result.data.perPage == PER_PAGE);
			
			assertTrue("result.data.photos.length > 0", result.data.photos.length > 0);
			
			var photo:Photo = result.data.photos[0];
			
			assertNotNull("photo.date is null", photo.date);
			assertNotNull("photo.match is null", photo.match);
			assertNotNull("photo.thumbnail is null", photo.thumbnail);
			assertNotNull("photo.id is null", photo.id);
			assertNotNull("photo.link is null", photo.link);
			
			assertNotNull("photo.place is null", photo.place);
			
			assertNotNull("photo.place.id is null", photo.place.id);
			assertNotNull("photo.place.name is null", photo.place.name);
			
			var pos:Position = photo.place.position;
			
			assertNotNull("position is null", pos);
			assertNotNull("pos.latitude is null", pos.latitude);
			assertNotNull("pos.longitude is null", pos.longitude);
			assertNotNull("pos.spread is null", pos.spread);
			
			assertNotNull("photo.projected is null", photo.projected);
			//assertTrue("photo.projected.spread != 0", photo.projected.spread != 0);
			assertTrue("photo.projected.x != 0", photo.projected.x != 0);
			assertTrue("photo.projected.y != 0", photo.projected.y != 0);
			assertTrue("photo.projected.theta != 0", photo.projected.theta != 0);
			
			var owner:Owner = photo.owner;
			
			assertNotNull("owner is null", owner);
			assertNotNull("owner.icon is null", owner.icon);
			assertNotNull("owner.link is null", owner.link);
			assertNotNull("owner.nsid is null", owner.nsid);
			assertNotNull("owner.username is null", owner.username);
		}
		
		public function testGetPlaceInfo():void
		{
			var m:MapprService = new MapprService(MapprServiceTest.API_KEY);
				m.addEventListener(MapprResultEvent.ON_GET_PLACE_INFO, addAsync(onGetPlaceInfo, CALL_TIMEOUT));
			
				m.getPlaceInfo(PLACE_ID);			
		}
		
		private function onGetPlaceInfo(result:MapprResultEvent):void
		{
			assertNotNull("result.data.places is null", result.data.places);
			assertTrue("result.data.places.length > 0", result.data.places.length > 0);

			var p:Place = result.data.places[0];
			
			assertNotNull("p.name is undefined", p.name);
			assertNotNull("p.fullName is undefined", p.fullName);
			assertNotNull("p.position is undefined", p.position);
			assertNotNull("p.consideration is undefined", p.considerations);
			
			assertNotNull("p.id is undefined", p.id);
			assertNotNull("p.type is undefined", p.type);
			assertNotNull("p.provenance is undefined", p.provenance);
			
			assertNotNull("p.lineage is undefined", p.lineage);
			assertTrue("p.lineage.length > 0", p.lineage.length > 0);	
		}
		
		public function testMatch():void
		{
			var m:MapprService = new MapprService(MapprServiceTest.API_KEY);
				m.addEventListener(MapprResultEvent.ON_MATCH_RESULT, addAsync(onMatch, CALL_TIMEOUT));
			
				m.match(TAGS, LIMIT);
		}
		
		private function onMatch(result:MapprResultEvent):void
		{
			assertNotNull("result.data.places is null", result.data.places);
			assertTrue("result.data.places.length > 0", result.data.places.length > 0);

			var p:Place = result.data.places[0];
			
			assertNotNull("p.name is undefined", p.name);
			assertNotNull("p.fullName is undefined", p.fullName);
			assertNotNull("p.position is undefined", p.position);
			assertNotNull("p.consideration is undefined", p.considerations);
			
			assertNotNull("p.match is undefined", p.match);
			assertTrue("p.match.votes != -1", p.match.votes != -1);
			
			assertNotNull("p.id is undefined", p.id);
			assertNotNull("p.type is undefined", p.type);
			assertNotNull("p.provenance is undefined", p.provenance);
			
			assertNotNull("p.lineage is undefined", p.lineage);
			assertTrue("p.lineage.length > 0", p.lineage.length > 0);			
		}
		
	}
}